<template>
  <div id="user">
    <Loading v-if="total == null"></Loading>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">标签管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>标签列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索标签区域 -->
      <el-row :gutter="20">
        <el-col :span="8"
          ><el-input
            placeholder="请输入标签名关键字查询"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="getTagList"
          >
            <template #append>
              <el-button icon="el-icon-search" @click="getTagList"></el-button>
            </template> </el-input
        ></el-col>
        <el-col :span="4">
          <el-button type="primary" @click="isShowTagDialog = true"
            >添加标签</el-button
          >
        </el-col>
      </el-row>

      <!-- 标签列表 -->
      <el-table :data="tagList" border stripe class="mt10">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="标签名" prop="name"></el-table-column>
        <el-table-column label="icon图标" prop="icon"></el-table-column>
        <el-table-column label="操作" width="130px">
          <template #default="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editTag(scope.row._id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteTag(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加/修改标签对话框 -->
    <el-dialog
      :title="tagForm._id ? '修改标签信息' : '添加新标签'"
      v-model="isShowTagDialog"
      width="400px"
      @close="tagDialogClose"
    >
      <el-form :rules="tagFormRules" :model="tagForm" label-width="70px">
        <el-form-item label="标签名" prop="tagName">
          <el-input v-model="tagForm.name"></el-input>
        </el-form-item>
        <el-form-item label="icon图标">
          <el-input v-model="tagForm.icon"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="isShowTagDialog = false">取 消</el-button>
          <el-button type="primary" @click="saveTag">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from "@vue/reactivity";
import { tagFormRules } from "../../data/rules";
import { postData, getData, putData, deleteData } from "../../services/http";
import { getCurrentInstance, onBeforeMount } from "@vue/runtime-core";
export default {
  name: "Tag",
  setup() {
    const { proxy } = getCurrentInstance();
    const state = reactive({
      tagForm: {},
      tagList: [],
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 10,
      },
      total: null,
    });
    const isShowTagDialog = ref(false);
    const stateAsRefs = toRefs(state);
    const getTagList = async () => {
      const { data: res } = await getData("rest/tag", {
        params: state.queryInfo,
      });
      state.tagList = res.data;
      state.total = res.total;
    };
    const saveTag = async () => {
      const id = state.tagForm._id;
      if (id) {
        await putData(`rest/tag/${id}`, state.tagForm, "修改成功");
      } else {
        await postData(
          "rest/tag",
          state.tagForm,
          `已成功添加标签:${state.tagForm.name}`
        );
      }
      getTagList();
      isShowTagDialog.value = false;
    };
    const tagDialogClose = () => {
      state.tagForm = {};
    };
    const editTag = async (id) => {
      const res = await getData(`rest/tag/${id}`);
      state.tagForm = res.data;
      isShowTagDialog.value = true;
    };
    const deleteTag = (row) => {
      const { name, _id } = row;
      proxy
        .$confirm(`此操作将永久删除标签 ${name}, 是否继续?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(async () => {
          await deleteData(`rest/tag/${_id}`, `已成功删除标签 ${name}`);
          getTagList();
        })
        .catch(() => {
          proxy.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    };

    const handleCurrentChange = (newPage) => {
      state.queryInfo.pageNum = newPage;
      getTagList();
    };
    const handleSizeChange = (newSize) => {
      state.queryInfo.pageSize = newSize;
      getTagList();
    };
    onBeforeMount(() => {
      getTagList();
    });
    return {
      ...stateAsRefs,
      tagFormRules,
      saveTag,
      deleteTag,
      tagDialogClose,
      editTag,
      isShowTagDialog,
      handleCurrentChange,
      handleSizeChange,
      getTagList,
    };
  },
};
</script>

<style>
</style>